/*
Copyright (c) 2012 - 2016 Qualcomm Technologies International, Ltd.

*/

/*!
@file
@ingroup sink_app
@brief
    Header file for interface with Subwoofer Audio Transfer library
*/

#ifndef _SINK_SWAT_H_
#define _SINK_SWAT_H_

typedef enum __subwoofer_latency_type
{
    subwoofer_latency_not_available,
    subwoofer_latency_standard,
    subwoofer_latency_low
} subwoofer_latency_type_t;

#ifdef ENABLE_SUBWOOFER

/* Application includes */
#include "sink_debug.h"
#include "sink_configmanager.h"
#include "sink_class_of_device.h"

/* Library includes */
#include <swat.h>
#include <bdaddr.h>
#include <connection.h>

/* SWAT Debug macro */
#ifdef DEBUG_SWAT
#define SWAT_DEBUG(x) DEBUG(x)
#else
#define SWAT_DEBUG(x)
#endif

/* Subwoofer COD = Service classes: Audio & Rendering, Major device class: Audio/Video Device, Minor Device class: Loudspeaker */
#define SUBWOOFER_CLASS_OF_DEVICE (AUDIO_MAJOR_SERV_CLASS | AV_COD_RENDER | AV_MAJOR_DEVICE_CLASS | AV_MINOR_SPEAKER) 

#define SW_MAX_REMOTE_DEVICES   1           /* The number of remote devices SWAT will support */
#define SW_MAX_INQUIRY_DEVS     5           /* The maximum number of subwoofer devices to return for inquiry mode */
#define SW_MAX_INQUIRY_TIME     10          /* The maximum time to enable inquiry mode when searching for subwoofer devices */
#define INQUIRY_LAP             0x9E8B33    /* The General/Unlimited Inquiry Access Code */
#define SW_INQUIRY_RETRY_TIME   11000       /* time after which another subwoofer inquiry is performed */
#define SW_INQUIRY_ATTEMPTS     3           /* number of times to try an inquiry scan */
#define SUB_WOOFER_CONNECT_RETRY_INTERVAL 30000 /* time before attempting another connection to the sub, allow other connections/pairing to complete */
#define SUB_TRIM_MAX            16          /* The maximum number of subwoofer trim volumes */
#define DEFAULT_SUB_TRIM_INDEX  10
#define SUBWOOFER_MUTE_DELAY    50          /* time required for subwoofer to reach mute state in milliseconds */
#define SUBWOOFER_LINK_SUPERVISION_TIMEOUT 1600 /* 1 second link supervision timeout */
/* 
    Defines the data structure for volume change messages
*/
typedef struct
{
    int16              new_volume;   /* subwoofer volume change level */
} SWAT_VOLUME_CHANGE_MSG_T;


/*
    Defines the data structure used for the Subwoofer configuration data
*/
typedef struct
{
    bdaddr              bd_addr;        /* Bluetooth address of the paired subwoofer device */
    sync_config_params  esco_params;    /* Custom eSCO SWAT parameters - if required */
} subwooferConfig_t;

/*************************************************************************
NAME    
    handleSwatMessage
    
DESCRIPTION
    Function to handle messages generated by the SWAT library
*/
void handleSwatMessage(Task task, MessageId id, Message message);


/*************************************************************************
NAME
    handleSubwooferGetAuthDevice
    
DESCRIPTION
    Handle CL_SM_GET_AUTH_DEVICE_CFM for subwoofer devices
*/
void handleSubwooferGetAuthDevice(const CL_SM_GET_AUTH_DEVICE_CFM_T * cfm);


/*************************************************************************
NAME    
    handleEventUsrSubwooferStartInquiry
    
DESCRIPTION
    Function to handle the user event to start an inquiry search for
    a subwoofer device
*/
void handleEventUsrSubwooferStartInquiry(void);


/*************************************************************************
NAME    
    handleSubwooferInquiryResult
    
DESCRIPTION
    Function to handle when an Inquiry result is returned from CL when in 
    inquiry_action rssi_subwoofer.
*/
void handleSubwooferInquiryResult( CL_DM_INQUIRE_RESULT_T* result );


/*************************************************************************
NAME    
    handleEventSysSubwooferConnect
    
DESCRIPTION
    Function to handle the user event for connecting a subwoofer device
*/
void handleEventSysSubwooferCheckPairing(void);


/*************************************************************************
NAME    
    handleEventSysSubwooferOpenLLMedia
    
DESCRIPTION
    Function to handle the user event for opening a Low Latency SWAT 
    media channel to the subwoofer device
*/
void handleEventSysSubwooferOpenLLMedia(void);


/*************************************************************************
NAME    
    handleEventSysSubwooferOpenStdMedia
    
DESCRIPTION
    Function to handle the user event for opening a Standard Latency SWAT 
    media channel to the subwoofer device
*/
void handleEventSysSubwooferOpenStdMedia(void);


/*************************************************************************
NAME    
    handleEventSysSubwooferCloseMedia
    
DESCRIPTION
    Function to handle the user event for closing the open SWAT media
    channel to the subwoofer device
*/
void handleEventSysSubwooferCloseMedia(void);


/*************************************************************************
NAME    
    handleEventSysSubwooferStartStreaming
    
DESCRIPTION
    Function to handle the user event to start streaming audio data over
    the open SWAT media channel to the subwoofer device
*/
void handleEventSysSubwooferStartStreaming(void);


/*************************************************************************
NAME    
    handleEventSysSubwooferSuspendStreaming
    
DESCRIPTION
    Function to handle the user event to suspend audio streaming over the
    open SWAT media channel to the subwoofer device
*/
void handleEventSysSubwooferSuspendStreaming(void);


/*************************************************************************
NAME    
    handleEventSysSubwooferDisconnect
    
DESCRIPTION
    Function to handle the user event disconnect a connected subwoofer
    device
*/
void handleEventSysSubwooferDisconnect(void);


/*************************************************************************
NAME    
    handleEventUsrSubwooferVolumeDown
    
DESCRIPTION
    Function to handle the user event to decrease the subwoofer volume
*/
void handleEventUsrSubwooferVolumeDown(void);


/*************************************************************************
NAME    
    handleEventUsrSubwooferVolumeUp
    
DESCRIPTION
    Function to handle the user event to decrease the subwoofer volume
*/
void handleEventUsrSubwooferVolumeUp(void);


/*************************************************************************
NAME    
    handleEventUsrSubwooferDeletePairing
    
DESCRIPTION
    Function to handle the user event to delete the subwoofer pairing info
    from the soundbar PDL
*/
void handleEventUsrSubwooferDeletePairing(void);

/*************************************************************************
NAME    
    handleEventUsrSubwooferDisconnect
    
DESCRIPTION
    Function to handle the user event to disconnect subwoofer 
*/
void handleEventUsrSubwooferDisconnect(void);

/*************************************************************************
NAME    
    updateSwatVolume
    
DESCRIPTION
    Function to update the SWAT system volume that is also sent to the 
    subwoofer device to keep volume sync'd
*/
void updateSwatVolumeNow(int16 new_volume);


/*************************************************************************
NAME    
    deleteSubwooferPairing

PARAMETERS
    delete_link_key - Flag to also delete the link key, otherwise will
    only delete the PS Data from subwoofer config block.

DESCRIPTION
    Function to delete the subwoofer pairing informtion from the soundbar
*/
void deleteSubwooferPairing(typed_bdaddr* dev_addr, bool delete_link_key);

/****************************************************************************
NAME    
    sinkSwatInit
    
DESCRIPTION
    Function to initialize SubWoofer config params.
RETURNS
    TRUE or FALSE
*/
bool sinkSwatInit(void);
/****************************************************************************
NAME    
    sinkSwatGetBdaddr
    
DESCRIPTION
    Function to write SubWoofer bd addr.
RETURNS
    bool
*/
bool sinkSwatWriteSubwooferBdaddr( const bdaddr * addr );
/****************************************************************************
NAME    
    sinkSwatGetBdaddr
    
DESCRIPTION
    Helper function to get SubWoofer bd addr.
RETURNS
    pointer to bdaddr
*/
bdaddr* sinkSwatGetBdaddr(void);
/****************************************************************************
NAME    
    sinkSwatGetEscoTxBandwidth
    
DESCRIPTION
    Helper function to get SubWoofer Tx parameters.
RETURNS
    uint32
*/
uint32 sinkSwatGetEscoTxBandwidth(void);
/****************************************************************************
NAME    
    sinkSwatGetEscoRxBandwidth
    
DESCRIPTION
    Helper function to get SubWoofer Rx parameters.
RETURNS
    uint32
*/
uint32 sinkSwatGetEscoRxBandwidth(void);
/****************************************************************************
NAME    
    sinkSwatGetEscoMaxLatency
    
DESCRIPTION
    Helper function to get SubWoofer esco max latency.
RETURNS
    uint16
*/
uint16 sinkSwatGetEscoMaxLatency(void);
/****************************************************************************
NAME    
    sinkSwatGetEscoVoiceSettings
    
DESCRIPTION
    Helper function to get SubWoofer esco voice settings.
RETURNS
    uint16
*/
uint16 sinkSwatGetEscoVoiceSettings(void);
/****************************************************************************
NAME    
    sinkSwatSetCheckPairing
    
DESCRIPTION
    Helper function to set SubWoofer Pairing status.
*/
void sinkSwatSetCheckPairing(bool value);
/****************************************************************************
NAME    
    sinkSwatCheckPairing
    
DESCRIPTION
    Helper function to check SubWoofer Pairing.
*/
bool sinkSwatCheckPairing(void);

/****************************************************************************
NAME    
    sinkSwatGetInquiryAttempts
    
DESCRIPTION
    Helper function to get SubWoofer Inquiry Attempts.
*/
uint8 sinkSwatGetInquiryAttempts(void);

/****************************************************************************
NAME    
    sinkSwatSetInquiryAttempts
    
DESCRIPTION
    Helper function to set SubWoofer Inquiry Attempts.
*/
void sinkSwatSetInquiryAttempts(uint8 value);

/****************************************************************************
NAME    
    sinkSwatGetDevId
    
DESCRIPTION
    Helper function to get SubWoofer Device Id.
*/

uint8 sinkSwatGetDevId(void);

/****************************************************************************
NAME    
    sinkSwatSetDevId
    
DESCRIPTION
    Helper function to set SubWoofer Device Id.
*/
void sinkSwatSetDevId(uint8 value);

/****************************************************************************
NAME    
    sinkSwatGetSubtrimIdx
    
DESCRIPTION
    Helper function to get SubWoofer Trim Id.
*/
uint8 sinkSwatGetSubtrimIdx(void);

/****************************************************************************
NAME    
    sinkSwatSetSubtrimIdx
    
DESCRIPTION
    Helper function to set SubWoofer Trim Id.
*/
void sinkSwatSetSubtrimIdx(uint8 value);

/****************************************************************************
NAME    
    sinkSwatGetSubtrim
    
DESCRIPTION
    Helper function to get SubWoofer Trim.
*/
uint8 sinkSwatGetSubtrim(void);

/****************************************************************************
NAME    
    sinkSwatSetSubtrim
    
DESCRIPTION
    Helper function to set SubWoofer Trim.
*/
void sinkSwatSetSubtrim(uint8 value);

/****************************************************************************
NAME    
    sinkSwatGetVolume
    
DESCRIPTION
    Helper function to get SubWoofer volume.
*/
uint8 sinkSwatGetVolume(void);

/****************************************************************************
NAME    
    sinkSwatSetVolume
    
DESCRIPTION
    Helper function to set SubWoofer volume.
*/
void sinkSwatSetVolume(uint8 value);

/****************************************************************************
NAME    
    sinkSwatIsEnabledRSSIpairing
    
DESCRIPTION
    Helper function to check if subwoofer RSSI pairing is enabled or not to ensure
	signal strenght is higher than the configured threshold.

RETURNS
   TRUE or FALSE
*/
bool sinkSwatIsEnabledRSSIpairing(void);
/****************************************************************************
NAME    
    sinkSwatGetSyncConfigParams
    
DESCRIPTION
    Function to set Subwoofer config params.
RETURNS
    Pointer to sync config params
*/
sync_config_params * sinkSwatGetSyncConfigParams(void);

/*****************************************************************************/

#endif /* ENABLE_SUBWOOFER */

/*************************************************************************
NAME    
    sendSampleRateToSub
    
DESCRIPTION
    Function to send the sample rate (in use) to the subwoofer
*/
#ifdef ENABLE_SUBWOOFER
void sendSampleRateToSub(uint16 sample_rate);
#else
#define sendSampleRateToSub(x)  ((void)0)
#endif

/****************************************************************************
NAME
    subwooferCheckConnection

DESCRIPTION
	check the sub woofer connection and streaming status and reconnect it if necessary

RETURNS
    None
*/
#ifdef ENABLE_SUBWOOFER
void subwooferCheckConnection(bool okToConnectSub);
#else
#define subwooferCheckConnection(x)
#endif

/****************************************************************************
NAME
    subwooferGetLatencyBasedOnSource

DESCRIPTION
	Derive the correct latency link based on the source used.

RETURNS
    subwoofer_latency_type_t
*/
#ifdef ENABLE_SUBWOOFER
subwoofer_latency_type_t subwooferGetLatencyBasedOnSource(audio_sources source);
#else
#define subwooferGetLatencyBasedOnSource(x) (FALSE)
#endif

/*************************************************************************
NAME    
    sendMuteToSubwoofer
    
DESCRIPTION
    Function to set the subwoofer to mute state to reduce pops when changing sources
*/
#ifdef ENABLE_SUBWOOFER
void sendMuteToSubwoofer(void);
#else
#define sendMuteToSubwoofer()  ((void)(0))
#endif

/*************************************************************************
NAME
    updateSwatVolume

DESCRIPTION
    Function to update the SWAT system volume that is also sent to the
    subwoofer device to keep volume sync'd
*/
#ifdef ENABLE_SUBWOOFER
void updateSwatVolume(int16 new_volume);
#else
#define updateSwatVolume(x) ((void)(x))
#endif

#ifdef ENABLE_SUBWOOFER
void subwooferResetSubwooferLinkType(void);
#else
#define subwooferResetSubwooferLinkType()   ((void)0)
#endif

#endif /* _SINK_SWAT_H_ */
